<?php

function experts_add_page($id) {
    return drupal_get_form('experts_edit_form',$id);
}

function experts_edit_page($id) {
    return drupal_get_form('experts_edit_form', $id);
}

function experts_edit_form($form, &$form_state) {
    $id = isset($form_state['build_info']['args'][0]) ? $form_state['build_info']['args'][0] : NULL;
    if (!empty($id)) {
        $record = nonghua_data_load('experts', 'experts_id', $id);
        if (empty($record)) {
            drupal_set_message(t('专家不存在！'), 'status', FALSE);
            return $form;
        }
    }
    // 判断用户角色
    if(!nonghua_user_access('experts', 'view', $id, !empty($record) ? $record->author_uid:0, 0) ){
        drupal_set_message(t('您没有权限查看本条数据！'), 'status', FALSE);
        return $form;
    }
    $form['experts_name'] = array(
        '#type' => 'textfield',
        '#title' => '专家姓名',
        '#default_value' => !empty($record) ? $record->experts_name : '',
        '#required' => true,
        '#maxlength' => 20,
    );
    $form['address'] = array(
        '#type' => 'textfield',
        '#title' => '地址',
        '#default_value' => !empty($record) ? $record->address : '',
        '#maxlength' => 80,
//        '#required' => true
    );
    $form['cell'] = array(
        '#type' => 'textfield',
        '#title' => '手机',
        '#default_value' => !empty($record) ? $record->cell : '',
        '#maxlength' => 20,
//        '#required' => true
    );
    $form['email'] = array(
        '#type' => 'textfield',
        '#title' => '邮箱',
        '#default_value' =>!empty($record) ? $record->email : '',
        '#size' => 32,
        '#maxlength' => 40,
    );
    $form['region'] = array(
        '#type' => 'textfield',
        '#title' => '区域',
        '#default_value' => !empty($record) ? $record->region : '',
        '#maxlength' => 20,
//        '#required' => true
    );
    $form['description'] = array(
        '#type' => 'textarea',
        '#title' => '专长描述',
        '#default_value' => !empty($record) ? $record->description : '',
        '#maxlength' => 500,
        '#attributes'=>array('placeholder'=>'最多输入500字。')
//        '#required' => true
    );
    $form['qualification'] = array(
        '#type' => 'textarea',
        '#title' => '职业资格',
        '#default_value' => !empty($record) ? $record->qualification : '',
        '#maxlength' => 500,
//        '#required' => true
        '#attributes'=>array('placeholder'=>'最多输入500字。')
    );
    $form['comment'] = array(
        '#type' => 'textarea',
        '#title' => '职业描述',
        '#default_value' => !empty($record) ? $record->comment : '',
        '#maxlength' => 500,
//        '#required' => true
        '#attributes'=>array('placeholder'=>'最多输入500字。')
    );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => arg(1) != 'add' ? '修改' : '添加',
        '#access' => nonghua_user_access('experts', 'edit', $id, !empty($record) ? $record->author_uid:0, 0)
    );
    return $form;
}

function experts_edit_form_validate($form, &$form_state) {
    if(!empty($form_state['values']['email'])){
        $regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';
        if (!preg_match($regex, $form_state['values']['email'])) {
            form_set_error('email', '邮箱地址不正确');
        }
    }
}

function experts_edit_form_submit($form, &$form_state) {
    global $user;
    $id = isset($form_state['build_info']['args'][0]) ? $form_state['build_info']['args'][0] : NULL;
    clear_search_autocomplete_cach();
    if (arg(1) == 'add') { //add new experts
        $nid = db_insert('experts') // Table name no longer needs {}
                ->fields(array(
                    'experts_name' => $form_state['values']['experts_name'],
                    'address' => $form_state['values']['address'],
                    'cell' => $form_state['values']['cell'],
                    'email' => $form_state['values']['email'],
                    'region' => $form_state['values']['region'],
                    'description' => $form_state['values']['description'],
                    'qualification' => $form_state['values']['qualification'],
                    'comment' => $form_state['values']['comment'],
                    'author_uid' => $user->uid,
                    'timestamp' => time(),))
                ->execute();
        if ($nid > 0) {
            drupal_set_message(t('添加成功！'), 'status', FALSE);
            drupal_goto('experts/add/'.$nid);
        }

    } else {
        $nid = db_update('experts')// edit exsting store. todo: update only modified fields
        ->fields(array(
            'experts_name' => $form_state['values']['experts_name'],
            'address' => $form_state['values']['address'],
            'cell' => $form_state['values']['cell'],
            'email' => $form_state['values']['email'],
            'region' => $form_state['values']['region'],
            'description' => $form_state['values']['description'],
            'qualification' => $form_state['values']['qualification'],
            'comment' => $form_state['values']['comment'],
//            'author_uid' => $user->uid,
            'timestamp' => time(),))
            ->condition('experts_id', $id, '=')
            ->execute();
        if ($nid > 0) {
            drupal_set_message(t('修改成功！'), 'status', FALSE);
        }

    }
}

